package model;
import geo2d.*; 
import processing.core.*;

public class Circle {
	pt O; 
	pt P; 
	public float R;
	float R0; 
	float dR; 
	String label; 
	private Circle(pt o, float r, float dr, String l){
		O = o; 
		P = new pt(O);
		R0 = r; 
		R = r; 
		dR = dr; 
		label = l; 
	}
	public static Circle Root(float h, float w, String l){
		pt o = new pt(h/2, w/2); 
		float r = PApplet.min(h, w); 
		return new Circle(o, r, 0, l); 
	}
	private static pt random_pt(Circle root){
		float x = (float) ((Math.random()-0.5)*root.R); 
		float y = (float) ((Math.random()-0.5)*root.R); 
		float d = PApplet.sqrt(PApplet.sq(x)+PApplet.sq(y));  
		while (d> root.R/2){
			x = (float) ((Math.random()-0.5)*root.R); 
			y = (float) ((Math.random()-0.5)*root.R);
			d = PApplet.sqrt(PApplet.sq(x)+PApplet.sq(y)); 
		}
		pt o = new pt(x, y); 
		o.add(root.O); 
		return o; 
	}
	public static Circle Random(Circle root, float r, float dr, String l){
		pt o = random_pt(root); 
		return new Circle(o, r, dr, l); 
	}
	public void sink(Circle root){
		//calc the gravitation from the root to keep the circle in the container
		vec v = new vec(O, root.O);
		float d = PApplet.sqrt(root.O.disTo(O));
		O.add(d*0.001f, v);
		//sink2(root); 
	}
	public void sink2(Circle root){
		vec vy = new vec(0, root.O.y-O.y); 
		O.add(0.005f, vy); 
	}
	public static void repel(Circle C1, Circle C2){
		//calc the repel force from peers to keep them apart
		float d = (C1.O).disTo(C2.O); 
		float dr = (C1.R+C2.R)/2; 
		if (d>dr) return; 
		float f = (dr-d)/dr; 
		vec v = new vec(C1.O, C2.O);
		C2.O.add(f, v); 
		C1.O.add(-f, v); 
	}
	float t=(float) (Math.random()*PApplet.TWO_PI); 
	//float t =0; 
	public void grow(int speed){
		if (t>PApplet.TWO_PI) {t-=PApplet.TWO_PI; }
		R=R0+dR*PApplet.sin(t); 
		t+=speed*0.01f; 
	}
	public boolean isIn(pt m){
		if (m.disTo(P)<R/2) return true; 
		return false; 
	}
	public void show(PApplet pa){
		if (Math.abs(P.x-O.x) > 5 || Math.abs(P.y-O.y) > 5) P.setTo(O); 
		P.show(R, pa);
	}; 
	public void showLabel(PApplet pa){
		//TODO
		pa.text(label, O.x, O.y); 
	}
	
}
